package com.malamute.core.listener;

import java.util.ArrayList;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import com.malamute.core.constant.SystemConstant;

/**
 * @author Aceguan
 * @description 当前在线用户
 * @date 2012-2-20 下午11:43:42
 * @version v1.0
 */
public class OnlineUserListener implements ServletContextListener,
		HttpSessionListener, HttpSessionAttributeListener {
	// 声明一个ServletContext对象
	private ServletContext application = null;

	/*
	 * (non-Javadoc)
	 * 
	 * @param se
	 * 
	 * @see
	 * javax.servlet.http.HttpSessionAttributeListener#attributeAdded(javax.
	 * servlet.http.HttpSessionBindingEvent)
	 */
	@Override
	// session范围属性添加时调用
	public void attributeAdded(HttpSessionBindingEvent se) {
		System.out.println("attributeAdded.....................");
		// 获得当前所有的用户
		ArrayList<String> allUser = (ArrayList<String>) application
				.getAttribute(SystemConstant.CTX_ALL_LOGIN);
		// 获得添加的用户
		if (SystemConstant.SES_USER_LOGIN.equals(se.getName())) {
			System.out.println(se.getName());
			String user = (String) se.getValue();
			// 添加到所有用户中
			allUser.add(user);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param se
	 * 
	 * @see
	 * javax.servlet.http.HttpSessionAttributeListener#attributeRemoved(javax
	 * .servlet.http.HttpSessionBindingEvent)
	 */
	@Override
	// session范围属性移除时调用
	public void attributeRemoved(HttpSessionBindingEvent se) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param se
	 * 
	 * @see
	 * javax.servlet.http.HttpSessionAttributeListener#attributeReplaced(javax
	 * .servlet.http.HttpSessionBindingEvent)
	 */
	@Override
	// session范围属性替换时调用
	public void attributeReplaced(HttpSessionBindingEvent se) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param se
	 * 
	 * @see
	 * javax.servlet.http.HttpSessionListener#sessionCreated(javax.servlet.http
	 * .HttpSessionEvent)
	 */
	@Override
	// session创建时调用该方法
	public void sessionCreated(HttpSessionEvent se) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param se
	 * 
	 * @see
	 * javax.servlet.http.HttpSessionListener#sessionDestroyed(javax.servlet
	 * .http.HttpSessionEvent)
	 */
	@Override
	// session销毁时调用该方法
	public void sessionDestroyed(HttpSessionEvent se) {
		// 获得当前所有的用户
		ArrayList<String> allUser = (ArrayList<String>) application
				.getAttribute(SystemConstant.CTX_ALL_LOGIN);
		// 获得session删除的用户
		String user = (String) se.getSession().getAttribute(
				SystemConstant.SES_USER_LOGIN);
		// 删除用户
		allUser.remove(user);
		// 重新设置到application范围中
		application.setAttribute(SystemConstant.CTX_ALL_LOGIN, allUser);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param sce
	 * 
	 * @see
	 * javax.servlet.ServletContextListener#contextInitialized(javax.servlet
	 * .ServletContextEvent)
	 */
	@Override
	// ServletContext创建时调用该方法
	public void contextInitialized(ServletContextEvent sce) {
		// 储存所用用户名
		ArrayList<String> allUser = new ArrayList<String>();
		// 获得当前application对象
		application = sce.getServletContext();
		// 设置到application范围
		application.setAttribute(SystemConstant.CTX_ALL_LOGIN, allUser);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param sce
	 * 
	 * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.
	 * ServletContextEvent)
	 */
	@Override
	// ServletContext销毁时调用该方法
	public void contextDestroyed(ServletContextEvent sce) {
		// TODO Auto-generated method stub

	}

}
